package com.silverhaze.diamond.client.ui.filter;

import com.silverhaze.diamond.client.data.SpecificData;
import com.silverhaze.diamond.client.data.dao.DAOLocator;
import com.silverhaze.diamond.client.resources.ServiceResourceContainer;
import com.silverhaze.diamond.client.ui.UIStringConstants;
import com.silverhaze.diamond.client.ui.table.model.AbstractServiceTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Author: Ivan Babanin
 * Time: 21:47:47
 * Date: 20.05.2008
 * <p/>
 * This part of code is strictly confidential.
 */
public class DetailsFilterPanel extends AbstractFilterPanel {

    private JTextField nameField;

    public DetailsFilterPanel(AbstractServiceTableModel model, JToggleButton tBtn) {
        super(model, tBtn);

        init();
    }

    private void init() {
        nameField = createTextField(10);

        getTButton().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                nameField.setText("*");
                getModel().fillData(SpecificData.filterDetails("*", DAOLocator.getInstance().getDetails()));
            }
        });

        JButton clearBtn = new JButton(ServiceResourceContainer.UNDO);
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(6));
        hBox.add(new JLabel(UIStringConstants.NAME + ": "));
        hBox.add(Box.createHorizontalStrut(3));
        hBox.add(nameField);
        hBox.add(Box.createHorizontalStrut(6));
        hBox.add(clearBtn);
        hBox.add(Box.createHorizontalStrut(6));

        Box vBox = Box.createVerticalBox();
        vBox.add(hBox);
        vBox.add(Box.createVerticalStrut(5));

        add(vBox, BorderLayout.CENTER);
    }

    private JTextField createTextField(int symbolCount) {
        JTextField field = new JTextField(symbolCount);
        field.setText("*");
        field.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent event) {
                applyData();
            }
        });
        return field;
    }

    public void applyData() {
        String name = nameField.getText().trim();

        getModel().fillData(SpecificData.filterDetails(name, DAOLocator.getInstance().getDetails()));
        getModel().fireTableDataChanged();
    }
}

